home *** CD-ROM | disk | FTP | other *** search
/ Nejlepší hry / Nejlepsi hry.iso / hry / plane arcade / planearcade.exe / tank3.bmp / input.cpp < prev    next >
C/C++ Source or Header  |  2004-07-03  |  6KB  |  255 lines

  1. #include "main.h"
  2.  
  3. //-------------------------------------------------------------
  4. //GLOBAL
  5. //--------------------------------------------------------------
  6. INPUT Input;
  7.  
  8. //------------------------------------------------------------------
  9. // Name: INPUT()
  10. // Desc: konstruktor
  11. //------------------------------------------------------------------
  12. INPUT::INPUT()
  13. {
  14.     m_pKeyboard    = NULL; 
  15.     m_pMouse       = NULL;
  16.     m_pDirectInput = NULL;
  17.  
  18.     //inicializacia poli
  19.     for (int i=0;i<256;i++)
  20.     {
  21.         KeyPRESS[i] = false;
  22.         KeyDOWN[i] = false;
  23.     }
  24. }
  25.  
  26. //------------------------------------------------------------------
  27. // Name: Initialize()
  28. // Desc: inicializacia Dinput
  29. //------------------------------------------------------------------
  30. void INPUT::Initialize()
  31. {
  32.  
  33.  
  34.     LogPrint("Nastavujem Direct Input");
  35.  
  36.     //vytvori DirectInput Objekt
  37.     if(FAILED(DirectInput8Create(hIns, DIRECTINPUT_VERSION, 
  38.           IID_IDirectInput8, (void**)&m_pDirectInput, NULL))) 
  39.     {
  40.         LogPrint("   Nepodarilo sa vytvori¥ m_pDirectInput");
  41.     }
  42.     else
  43.     {
  44.         LogPrint("   m_pDirectInput vytvoreny");
  45.     }
  46.  
  47.  
  48.  
  49.     //KEYBOARD =======================================================================
  50.     //Create the keyboard device object
  51.     if(FAILED(m_pDirectInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL))) 
  52.     { 
  53.         LogPrint("   Nepodarilo sa nastavit klavesnicu");       
  54.     }    
  55.     else
  56.     {
  57.         LogPrint("   Klavesnica nastavena");        
  58.     }
  59.  
  60.     //Set the data format for the keyboard
  61.     if(FAILED(m_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
  62.     { 
  63.         LogPrint("   Nepodarilo sa nastavit format klavesnice");
  64.     }
  65.     else
  66.     {
  67.         LogPrint("   Format klavesnice nastaveny");
  68.     }
  69.  
  70.     //Set the cooperative level for the keyboard
  71.     if(FAILED(m_pKeyboard->SetCooperativeLevel(hWnd, 
  72.                                            DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
  73.     { 
  74.         LogPrint("   nepodarilo sa nastavit ccoperative level pre klavesnicu");
  75.     }
  76.     else
  77.     {
  78.         LogPrint("   cooperative level pre klavesnicu nastaveny");
  79.     }    
  80.  
  81.     //Acquire the keyboard
  82.     if(m_pKeyboard)
  83.     {
  84.         LogPrint("   Acquire klavesnica");
  85.         m_pKeyboard->Acquire(); 
  86.     }
  87.  
  88.  
  89.  
  90.     
  91.     //MOUSE =======================================================================
  92.     //Create the mouse device object
  93.     if(FAILED(m_pDirectInput->CreateDevice(GUID_SysMouse, &m_pMouse, NULL)))
  94.     { 
  95.         LogPrint("   Nepodarilo sa nastavit mys ");
  96.     }
  97.     else
  98.     {
  99.         LogPrint("   Mys nastavena");
  100.     }
  101.  
  102.     //Set the data format for the mouse
  103.     if(FAILED(m_pMouse->SetDataFormat(&c_dfDIMouse)))
  104.     { 
  105.         LogPrint("   Nepodarilo sa nastavit formaty mysi");
  106.     }
  107.     else
  108.     {
  109.         LogPrint("   Format myse nastavena");
  110.     }
  111.  
  112.     //Set the cooperative level for the mouse
  113.     if(FAILED(m_pMouse->SetCooperativeLevel(hWnd, 
  114.                                         DISCL_FOREGROUND|DISCL_NONEXCLUSIVE)))
  115.     { 
  116.         LogPrint("   nepodarilo sa nastavit ccoperative level pre mys");
  117.     }
  118.     else
  119.     {
  120.         LogPrint("   cooperative level pre mys nastaveny");
  121.     }
  122.  
  123.     //Acquire the mouse
  124.     if(m_pMouse)
  125.     {
  126.         LogPrint("   Acquire mys");
  127.         m_pMouse->Acquire(); 
  128.     }
  129.  
  130.     
  131. }
  132.  
  133.  
  134. //------------------------------------------------------------------
  135. // Name: CleanUp()
  136. // Desc: Odstrani DInput 
  137. //------------------------------------------------------------------
  138. void INPUT::CleanUp()
  139. {
  140.     if(m_pKeyboard)
  141.     {
  142.         m_pKeyboard->Unacquire(); 
  143.         m_pKeyboard->Release();
  144.     }
  145.  
  146.     if(m_pMouse)
  147.     {
  148.         m_pMouse->Unacquire();
  149.         m_pMouse->Release();
  150.     }
  151.  
  152.    if(m_pDirectInput != NULL)
  153.         m_pDirectInput->Release();
  154.  
  155. }
  156.  
  157.  
  158. //------------------------------------------------------------------
  159. // Name: RefreshKeyboard()
  160. // Desc: Ziska stlacene klavesy
  161. //------------------------------------------------------------------
  162. void INPUT::RefreshKeyboard()
  163. {
  164.     int i;
  165.  
  166.     //ziskanie KeyDown
  167.     m_pKeyboard->GetDeviceState(sizeof(KeyDown),
  168.                                           (LPVOID)&KeyDown);
  169.  
  170.     //ziskanie KeyDOWN a KeyPRESS
  171.     for (i=0;i<256;i++)
  172.     {
  173.         
  174.         if (KeyDOWN[i] == false)
  175.         {
  176.             KeyPRESS[i] =  (KEYDOWN(KeyDown,i));
  177.         }
  178.         else
  179.         {
  180.             KeyPRESS[i] = false;
  181.         }
  182.  
  183.         KeyDOWN[i] =  (KEYDOWN(KeyDown,i));
  184.  
  185.     }
  186.  
  187.   
  188. }
  189.  
  190. //------------------------------------------------------------------
  191. // Name: RefreshMouse()
  192. // Desc: Ziska info o mysi
  193. //------------------------------------------------------------------
  194. void INPUT::RefreshMouse()
  195. {
  196. #define MOUSEBUTTONDOWN(key) (key & 0x80)
  197. #define MOUSEBUTTON_LEFT 0
  198. #define MOUSEBUTTON_RIGHT 1
  199. #define MOUSEBUTTON_MIDDLE 2
  200.  
  201.  
  202.     DIMOUSESTATE MouseState;
  203.  
  204.     m_pMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState);
  205.  
  206.     //press-reset
  207.     MouseLeftPress = 0;
  208.     MouseRightPress = 0;
  209.  
  210.  
  211.     //Lave tlacitko
  212.     if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_LEFT]))
  213.     {
  214.         if (MouseLeftDown == 0)
  215.             MouseLeftPress = 1;
  216.  
  217.         MouseLeftDown = 1;
  218.     }
  219.     else
  220.     {
  221.         MouseLeftDown = 0;
  222.     }
  223.  
  224.  
  225.     //Prave tlacitko
  226.     if(MOUSEBUTTONDOWN(MouseState.rgbButtons[MOUSEBUTTON_RIGHT]))
  227.     {
  228.         if (MouseRightDown == 0)
  229.             MouseRightPress = 1;
  230.  
  231.         MouseRightDown = 1;
  232.     }
  233.     else
  234.     {
  235.         MouseRightDown = 0;
  236.     }
  237.  
  238.  
  239.     //solid
  240.     POINT MousePos;
  241.     ScreenToClient(hWnd,&MousePos);
  242.     GetCursorPos(&MousePos);
  243.     Mouse.X = (float)  MousePos.x ;
  244.     Mouse.Y = (float) MousePos.y ;
  245.     Mouse.Z = 0.0f;
  246.  
  247.     //relativne
  248.     MouseRelative.X = (float)MouseState.lX;
  249.     MouseRelative.Y = (float)MouseState.lY;
  250.     MouseRelative.Z = (float)MouseState.lZ;
  251.     
  252.  
  253. }
  254.  
  255.